package pers.karl.design.basic.strategy.usage.factory;

import pers.karl.design.basic.strategy.PriceStrategy;
import pers.karl.design.basic.strategy.usage.factory.enums.StrategyType;

import java.util.HashMap;
import java.util.Map;

/**
 * 策略工厂
 *
 *    放入工厂类的方式：
 *      主动存入： 策略类中添加存入方法、
 *      被动：包扫描、
 *
 *    由于调用方是通过枚举类获取策略实现类的，通过工厂的意义感觉并不大~
 *
 * @author karl
 */
public class StrategyFactory {

    private Map<StrategyType, PriceStrategy> priceStrategies = new HashMap<>();

    /**
     * 将策略加入到集合中，避免后期增加策略时，需要修改工厂类
     * @param strategyType
     * @param priceStrategy
     */
    public void register(StrategyType strategyType, PriceStrategy priceStrategy){
        if(strategyType == null) {
            throw new IllegalArgumentException("strategyType is null");
        }
        if(priceStrategy == null) {
            throw new IllegalArgumentException("priceStrategy is null");
        }

        priceStrategies.put(strategyType,priceStrategy);
    }

    /**
     * 获取价格策略
     * @param strategyType
     * @return
     */
    public PriceStrategy getPriceStrategy(StrategyType strategyType){
        return priceStrategies.get(strategyType);
    }

}
